import CryptoJS from 'crypto-js'

/**
 * 加密解密
 * @param string $str 需要加密/解密的字符串
 * @param string $code 密钥
 * @param bool $operation false表示加密 true表示解密
 */
export function endecrypt(str, operation=false) {
  var code = process.env.VUE_APP_BASE_API_KEY + process.env.VUE_APP_BASE_API_SECRET
  code = CryptoJS.MD5(code).toString();

  var iv = CryptoJS.enc.Utf8.parse(code.substring(0,16));
  var key = CryptoJS.enc.Utf8.parse(code.substring(16));
  if(operation){
    return JSON.parse(CryptoJS.AES.decrypt(str, key, {iv:iv, padding:CryptoJS.pad.Pkcs7}).toString(CryptoJS.enc.Utf8));
  }
  return CryptoJS.AES.encrypt(str, key, {iv:iv, mode:CryptoJS.mode.CBC, padding:CryptoJS.pad.Pkcs7}).toString();
}
